﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Data;
using System.Text;

public partial class LoanSelectionDialog : System.Web.UI.UserControl
{
    Unit mWidth = 820;
    string mTitleWindow = "LISTA IMPRUMUTURILOR";
    string appPath = string.Empty;


    public Unit Width
    {
        get { return mWidth; }
        set { mWidth = value; }
    }

    public string TitleWindow
    {
        get { return mTitleWindow; }
        set { mTitleWindow = value; }
    }      

    public int SelectedLoanID
    {
        get
        {
            int tempInt = 0;
            int.TryParse(selectedLoanIDHiddenField.Value, out tempInt);
            return tempInt;
        }
        set
        {
            selectedLoanIDHiddenField.Value = value.ToString();
        }
    }

    public void BindDataSource()
    {
        loanCategoriesTreeView.Nodes[0].ChildNodes.Clear();
        txtSearch.Value = string.Empty;

        DataTable categoriesDT = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.CreditState);

        if (categoriesDT != null && categoriesDT.Rows.Count > 0)
        {
            for (int i = 0; i < categoriesDT.Rows.Count; i++)
            {
                if ((int)categoriesDT.Rows[i]["Code"] != 0)
                {
                    TreeNode rotNode = loanCategoriesTreeView.Nodes[0];

                    TreeNode newNode = new TreeNode();
                    newNode.Text = categoriesDT.Rows[i]["Name"].ToString();
                    newNode.Value = categoriesDT.Rows[i]["Code"].ToString();

                    rotNode.ChildNodes.Add(newNode);
                }
            }
        }

        try
        {
            string nodeValue = ((int)Constants.Constants.Classifiers.CreditState_Activ).ToString();
            TreeNode node = Utils.FindNode(loanCategoriesTreeView, nodeValue);
            if (node != null)
            {
                node.Selected = true;
            }
            else
            {
                loanCategoriesTreeView.Nodes[0].Selected = true;
            }
        }
        catch
        { }
        FillLoansGridView();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WindowTitleLabel.Text = mTitleWindow;
        txtSearch.Attributes.Add("onkeyup", "Search('" + txtSearch.ClientID + "','" + loansGridView.ClientID + "')");
        loansListDiv.Attributes.Add("onscroll", "SetDivPosition('" + loansListDiv.ClientID + "')");
        RegisterAllClientJavaScripts();
    }

    private void RegisterAllClientJavaScripts()
    {
        if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), "setSelection" + this.ClientID))
        {
            StringBuilder cstext2 = new StringBuilder();
            cstext2.Append("<script type=text/javascript>      function setSelection" + this.ClientID + "(loanID, gridIndex) { ");
            cstext2.Append("   document.getElementById('" + selectedLoanIDHiddenField.ClientID + "').value = loanID; ");
            cstext2.Append("   var table = document.getElementById('" + loansGridView.ClientID + "');  ");
            cstext2.Append("   var rows = table.getElementsByTagName(\"tr\");   ");
            cstext2.Append("    for (i = 0; i < rows.length; i++) {");
            cstext2.Append("        if (gridIndex == i - 1){ rows[i].style.backgroundColor = \"#CCCCFF\"; } ");
            cstext2.Append("        else { rows[i].style.backgroundColor = \"\"; } ");
            cstext2.Append("        } ");
            cstext2.Append("    } ");            
            cstext2.Append("</script>");

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "checkAllScript" + this.ClientID, cstext2.ToString(), false);
        }
    }

    public void Show()
    {
        loanModalPopup.Show();
    }

    public void Hide()
    {
        loanModalPopup.Hide();
    }

    protected void okButton_Click(object sender, EventArgs e)
    {
        int selectedloan = 0;
        int.TryParse(selectedLoanIDHiddenField.Value, out selectedloan);

        if (selectedloan != 0)
        {
            FilterWindowEventsArg args = new FilterWindowEventsArg(selectedloan);
            OnClientSelected(this, args);
        }       
    }

    public class FilterWindowEventsArg : EventArgs
    {
        private int selectedItem;
        public int SelectedItem
        {
            get { return selectedItem; }
        }

        public FilterWindowEventsArg(int item)
        {
            selectedItem = item;
        }
    }

    public delegate void CustomItemEventHandler(object sender, FilterWindowEventsArg e);

    public event CustomItemEventHandler OnClientSelected;

    protected void FillLoansGridView()
    {
        loansGridView.SelectedIndex = -1;
        loansGridView.DataSource = null;
        loansGridView.DataBind();

        Credits.Module moduleCredits = Utils.ModuleCredits();

        if (moduleCredits != null)
        {
            int aproveStatus = 0;
            int.TryParse(loanCategoriesTreeView.SelectedValue, out aproveStatus);

            int clientID = 0;
            DataTable clientsDT = moduleCredits.GetLoansList(aproveStatus, clientID);
            loansGridView.DataSource = clientsDT;
            loansGridView.DataBind();
        }
    }

    protected void loansGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.Attributes["onclick"] = "setSelection" + this.ClientID + "('" + e.Row.Cells[0].Text + "', '" + e.Row.RowIndex + "');";           
        }
    }

    protected void loanCategoriesTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        FillLoansGridView();
    }
}